package cn.iocoder.yudao.module.erp.controller.admin.business.group.goodscategory;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.goodscategory.vo.GoodsCategoryPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.goodscategory.vo.GoodsCategoryRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.goodscategory.vo.GoodsCategorySaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.group.goodscategory.vo.GoodsCategoryTreeRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.group.goodscategory.GoodsCategoryDO;
import cn.iocoder.yudao.module.erp.service.business.group.goodscategory.GoodsCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物品类目")
@RestController
@RequestMapping("/erp/category")
@Validated
public class GoodsCategoryController {

    @Resource
    private GoodsCategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建物品类目")
    @PreAuthorize("@ss.hasPermission('erp:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody GoodsCategorySaveReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物品类目")
    @PreAuthorize("@ss.hasPermission('erp:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody GoodsCategorySaveReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物品类目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物品类目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:category:query')")
    public CommonResult<GoodsCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        GoodsCategoryDO category = categoryService.getCategory(id);
        return success(BeanUtils.toBean(category, GoodsCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物品类目分页")
    @PreAuthorize("@ss.hasPermission('erp:category:query')")
    public CommonResult<List<GoodsCategoryRespVO>> getCategoryPage(@Valid GoodsCategoryPageReqVO pageReqVO) {
        List<GoodsCategoryDO> list = categoryService.getCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(list, GoodsCategoryRespVO.class));
    }
    @GetMapping("/simple-list")
    @Operation(summary = "获取树的层级")
    @PreAuthorize("@ss.hasPermission('erp:category:query')")
    public CommonResult<List<GoodsCategoryTreeRespVO>> getCategoryList() {
        List<GoodsCategoryDO>  list = categoryService.getCategoryList();
        return success(BeanUtils.toBean(list, GoodsCategoryTreeRespVO.class));
    }

}